package things;

import Room.Room;
import java.awt.Dimension;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;

public class Player {

    public static int width;
    public static int height;
    public Graphics2D g2;
    public World w;
    Dimension imgSize, cellSize, cellCenter;
    public boolean smellWumpus = false;
    public int count;
    public int xLoc = 0;
    public int yLoc = 3;
    public int defaultWait = 0;

    public Player() {
    }

    /**
     * Create a new player
     * @param width Width of the world the player will be in
     * @param height Height of the world the player will be in 
     */
    public Player(World w) {
        this.w = w;
        g2 = w.g2;
        width = w.width;
        height = w.height;
    }

    public Player(World w, int x, int y) {
        this.w = w;
        g2 = w.g2;
        width = w.width;
        height = w.height;
        draw(x, y, w);
    }
    public ImageIcon ii = new ImageIcon("images/mm.gif");
    Dimension center = new Dimension(ii.getIconWidth() / 2, ii.getIconHeight() / 2);

    /**
     * Draws the player in the specified cell, given by x, y.
     * @param p The player to be drawn.
     * @param x Locaion of the cell where the player is to be drawn.
     * @param y Location of the cell where the player is to be drawn.
     * @param g2 Graphics object to draw on.
     */
    public void draw(int x, int y, World w) {
        if (x > 3 || x < 0 || y > 3 || y < 0) {
            throw new IndexOutOfBoundsException("Please use something between 0 and 3, inclusive");
        } else {
            cellSize = new Dimension((int) (w.width * 0.25), (int) (w.height * 0.25));
            cellCenter = new Dimension(cellSize.width * x + cellSize.width / 2, cellSize.height * y + cellSize.height / 2);
            imgSize = new Dimension(cellCenter.width - (this.ii.getIconWidth() / 2), cellCenter.height - (this.ii.getIconHeight() / 2));
            w.g2.drawImage(this.ii.getImage(), imgSize.width, imgSize.height, null);
        }
    }

    /**
     * Moves the player one cell to the right. Updates the location then repaints the world with
     * the player in new location then sleeps for whatever time was given.
     */
    public void moveRight() {
        w.sleep(333);
        if (w.x != 3) {
            w.x += 1;
            update();
            w.repaint();
            sleep();
        }

    }

    /**
     * Moves the player one cell to the left. Updates the location then repaints the world with
     * the player in new location then sleeps for whatever time was given.
     */
    public void moveLeft() {

        if (w.x != 0) {
            w.x -= 1;
            update();
            w.repaint();
            sleep();
        }

    }

    /**
     * Moves the player one cell down. Updates the location then repaints the world with
     * the player in new location then sleeps for whatever time was given.
     */
    public void moveDown() {
        if (w.y != 3) {
            w.y += 1;
            update();
            w.repaint();

        }
    }

    /**
     * Moves the player one cell up. Updates the location then repaints the world with
     * the player in new location then sleeps for whatever time was given.
     */
    public void moveUp() {
        if (w.y != 0) {
            w.y -= 1;
            update();
            w.repaint();
            sleep();
        }

    }

    /**
     * Updates the player of his own current location.
     */
    private void update() {
        xLoc = w.x;
        yLoc = w.y;
    }

    /**
     * Length of time between moves
     */
    private void sleep() {
        w.sleep(333);
    }

    /**
     * Does the player sense/see/whatever gold in the current room?
     * @return True if so. False if not
     */
    public boolean foundGold() {
        return w.rs[xLoc][yLoc].isGold;
    }

    /**
     * Does the player feel a breeze in his current room?
     * @return True if so. False if not
     */
    public boolean feelBreeze() {
        return w.rs[xLoc][yLoc].hasBreeze;
    }
}
